package danny.hoi.service.cargo.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import danny.hoi.dao.export.cargo.ContractDao;
import danny.hoi.dao.export.cargo.ContractProductDao;
import danny.hoi.dao.export.cargo.ExtCproductDao;
import danny.hoi.domain.export.cargo.*;
import danny.hoi.service.cargo.ContractProductService;
import danny.hoi.utils.UuidUtil;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ContractProductServiceImpl implements ContractProductService {
    @Autowired
    private ContractProductDao contractProductDao;
    @Autowired
    private ExtCproductDao extCproductDao;
    @Autowired
    private ContractDao contractDao;

    @Override
    public List<ContractProduct> findAll(ContractProductExample example) {
        return contractProductDao.selectByExample(example);
    }

    @Override
    public PageInfo<ContractProduct> findByPages(ContractProductExample example, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<ContractProduct> list = contractProductDao.selectByExample(example);
        //查询当前购销合同的货物和所有附件
        for (ContractProduct contractProduct : list) {
            ExtCproductExample extCproductExample = new ExtCproductExample();
            ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
            criteria.andContractProductIdEqualTo(contractProduct.getId());
            List<ExtCproduct> extCproductList = extCproductDao.selectByExample(extCproductExample);
            contractProduct.setExtCproducts(extCproductList);
        }

        return new PageInfo<>(list);
    }

    @Override
    public ContractProduct findById(String id) {
        return contractProductDao.selectByPrimaryKey(id);
    }

    @Override
    public void save(ContractProduct contractProduct) {
        contractProduct.setId(UuidUtil.getUuid());
        //根据货物中的购销合同id更新购销合同的总金额，总货物数
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        Double amout = 0d;
        if (contractProduct.getCnumber() != null && contractProduct.getPrice() != null) {
            amout = contractProduct.getCnumber() * contractProduct.getPrice();
        }
        contractProduct.setAmount(amout);
        contract.setTotalAmount(contract.getTotalAmount() + amout);
        contract.setProNum(contract.getProNum() + 1);
        //更新contract
        contractDao.updateByPrimaryKeySelective(contract);
        contractProductDao.insertSelective(contractProduct);
    }

    @Override
    public void update(ContractProduct contractProduct) {
        //计算修改后的货物金额
        Double amount = 0d;
        if (contractProduct.getCnumber() != null && contractProduct.getPrice() != null) {
            amount = contractProduct.getCnumber() * contractProduct.getPrice();
        }
        contractProduct.setAmount(amount);
        //查出原来的货物
        ContractProduct dbContractProduct =
                contractProductDao.selectByPrimaryKey(contractProduct.getId());
        //获取货物对应的购销合同
        Contract contract =
                contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //更新购销合同总金额
        contract.setTotalAmount(contract.getTotalAmount() - dbContractProduct.getAmount() + amount);
        contractDao.updateByPrimaryKeySelective(contract);

        contractProductDao.updateByPrimaryKeySelective(contractProduct);
    }

    @Override
    public void delete(String id) {
        //注意：更新购销合同的总金额
        //根据id 查询获取货物
        ContractProduct contractProduct = contractProductDao.selectByPrimaryKey(id);
        //根据购销合同id获取购销合同
        Contract contract = contractDao.selectByPrimaryKey(contractProduct.getContractId());
        //根据货物id货物所有的附件
        ExtCproductExample extCproductExample = new ExtCproductExample();
        ExtCproductExample.Criteria criteria = extCproductExample.createCriteria();
        criteria.andContractProductIdEqualTo(id);
        List<ExtCproduct> extCproductList =
                extCproductDao.selectByExample(extCproductExample);
        //计算当前货物总金额和附件总结
        Double amount = contractProduct.getAmount();
        int size = 0;
        if (extCproductList != null && extCproductList.size() > 0) {
            size = extCproductList.size();
            for (ExtCproduct extCproduct : extCproductList) {
                amount += extCproduct.getAmount();
                //删除附件
                extCproductDao.deleteByPrimaryKey(extCproduct.getId());
            }
        }
        //更新购销合同总金额,货物数,附件数
        contract.setTotalAmount(contract.getTotalAmount() - amount);
        contract.setProNum(contract.getProNum() - 1);
        contract.setExtNum(size);
        contractDao.updateByPrimaryKeySelective(contract);
        contractProductDao.deleteByPrimaryKey(id);
    }

    @Override
    public List<OutputQueryVo> findByShipTime(String loginCompanyId, String inputDate) {
        return contractProductDao.selectByCompanyIdAndShipTime(loginCompanyId, inputDate);
    }
}
